<?php

namespace app\service\controller;

use think\facade\Request;
use think\facade\Config;
use think\facade\Env;

/**
 *图片上传class
 */
class File
{

    /**
     * 单图上传
     * filename  html页面的input框name值
     * folder   为要上传到的文件里名  默认路径为 public/upload/folder
     * water  是否加水印
     * thumb  是否等比例缩放
     *
     */
    public function upload_single_img($filename, $folder = 'default', $quality = '', $water = false, $thumb = false, $thumb_w = 180, $thumb_h = 180)
    {
        $file = Request::instance()->file($filename);

        if ($file) {
            // 上传到文件夹
            $upload_path = Env::get('ROOT_PATH') . 'public' . DIRECTORY_SEPARATOR . 'uploads';
            !empty($folder) && $upload_path .= DIRECTORY_SEPARATOR . $folder;

            // 上传验证
            $info = $file->validate(['size' => Config::get('parm_config.img_max_size'), 'ext' => Config::get('parm_config.img_suffix')])->move($upload_path);
            if ($info) {
                // 成功上传后 获取上传信息
                $show_path = '/uploads/';
                !empty($folder) && $show_path .= $folder . '/';
                $img_path = $show_path . str_replace("\\", "/", $info->getSaveName());

                // 图像品质二次处理
                $this->img_quality('.' . $img_path, $quality);
                // 水印
                $water == true && $this->img_water('.' . $img_path);
                // 等比例缩放
                $thumb == true && $this->img_thumb('.' . $img_path, $thumb_w, $thumb_h);

                return ['code' => 1, 'img_path' => $img_path];
            } else {
                // 上传失败获取错误信息
                return ['code' => 0, 'msg' => $file->getError()];
            }
        }
        return true;
    }


    /**
     * 多图上传
     * filename  html页面的input框name值 数组
     * folder   为要上传到的文件里名  默认路径为 publi/upload/folder
     * water  是否加水印
     * thumb  是否等比例缩放
     *
     */
    public function upload_mulit_img($filename, $folder = 'default', $water = false, $thumb = false, $thumb_w = 180, $thumb_h = 180)
    {
        $files = Request::instance()->file($filename);
        if ($files) {
            // 上传到文件夹
            $upload_path = Env::get('ROOT_PATH') . 'public' . DIRECTORY_SEPARATOR . 'uploads';
            !empty($folder) && $upload_path .= DIRECTORY_SEPARATOR . $folder;
            $files_arr = [];

            foreach ($files as $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->validate(['size' => Config::get('parm_config.img_max_size'), 'ext' => Config::get('parm_config.img_suffix')])->move($upload_path);
                if ($info) {
                    // 成功上传后 获取上传信息
                    $show_path = '/uploads/';
                    !empty($folder) && $show_path .= $folder . '/';
                    $img_path = $show_path . str_replace("\\", "/", $info->getSaveName());

                    // 图像品质二次处理
                    $this->img_quality('.' . $img_path);
                    // 水印
                    $water == true && $this->img_water('.' . $img_path);
                    // 等比例缩放
                    $thumb == true && $this->img_thumb('.' . $img_path, $thumb_w, $thumb_h);
                    $files_arr[] = $img_path;
                } else {
                    // 上传失败获取错误信息
                    return ['code' => 0, 'msg' => $file->getError()];
                }
            }

            return ['code' => 1, 'img_path' => $files_arr];
        }
        return true;
    }


     /**
      * 图像品质  处理
      */
    protected function img_quality($img_path, $quality = '')
    {
        $image = \think\Image::open($img_path);
        $quality = empty($quality) ? Config::get('parm_config.img_quality') : $quality;
        $image->save($img_path, null, $quality);
    }

    /**
     * 图像水印  处理
     * */
    protected function img_water($img_path)
    {
        $image = \think\Image::open($img_path);
        $image->water('.' . Config::get('parm_config.img_water'), Config::get('parm_config.img_water_locate'), Config::get('parm_config.img_water_alpha'))->save($img_path);
    }

    /**
     * 图像裁剪 处理   默认剧中裁剪 180*180
     * */
    protected function img_thumb($img_path, $thumb_w, $thumb_h)
    {
        $image = \think\Image::open($img_path);
        $image->thumb($thumb_w, $thumb_h)->save($img_path);
    }
}

